Imports System.Xml
Public Class frmSearchDuplicates
    
    Private Sub frmSearchDuplicates_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim BookmarkFile As New XmlDocument
        BookmarkFile.Load("Bookmarks.xml")
      GoThroughAllBookmarks(BookmarkFile.DocumentElement, FindingDuplicates)
        lblStatus.Text = "Done!"
    End Sub

    Sub GoThroughAllBookmarks(ByVal inXmlNode As XmlNode, ByVal FindingDuplicates As Boolean)
        Dim xNode As XmlNode
        Dim nodeList As XmlNodeList
        Dim i As Integer
        If inXmlNode.HasChildNodes Then
            nodeList = inXmlNode.ChildNodes
            i = 0
            While i <= nodeList.Count - 1
                xNode = inXmlNode.ChildNodes(i)
                Dim NewBookmark As New TreeNode
                If xNode.Name <> "folder" Then
                    NewBookmark.Name = xNode.Attributes(1).Value & "\" & xNode.Attributes(0).Value
                    NewBookmark.Text = xNode.Attributes(0).Value
                    Select Case FindingDuplicates
                        Case True
                            SearchForDuplicates(inXmlNode, NewBookmark.Name, NewBookmark.Text)
                        Case False
                            SearchForDeadLinks(inXmlNode)
                            GoThroughAllBookmarks(xNode, FindingDuplicates)
                    End Select
                End If
                i += 1
            End While
        End If
    End Sub


    Sub SearchForDuplicates(ByVal inXmlNode As XmlNode, ByVal BookmarkPath As String, ByVal BookmarkName As String)
        Dim xNode As XmlNode
        Dim nodeList As XmlNodeList
        Dim i As Integer
        If inXmlNode.HasChildNodes Then
            nodeList = inXmlNode.ChildNodes
            i = 0
            While i <= nodeList.Count - 1
                xNode = inXmlNode.ChildNodes(i)
                Dim NewBookmark As New TreeNode
                If xNode.Name <> "folder" Then
                    NewBookmark.Name = xNode.Attributes(1).Value & "\" & xNode.Attributes(0).Value
                    NewBookmark.Text = xNode.Attributes(0).Value
                    'Now we have gotten the bookmark's info, it's time to search for a duplicate!
                    Dim SearchWebsite As String = AccessBookmarks.ReadBookmark(NewBookmark.Name)
                    Dim Website As String = AccessBookmarks.ReadBookmark(BookmarkPath)
                    If Website = SearchWebsite And NewBookmark.Name <> BookmarkPath Then
                        If CheckIfBookmarkAlreadyFlagged(NewBookmark.Name) = False Then
                            AddToListView(NewBookmark.Name, SearchWebsite, NewBookmark.Text)
                            If CheckIfBookmarkAlreadyFlagged(BookmarkPath) = False Then
                                AddToListView(BookmarkPath, Website, BookmarkName)
                            End If
                        End If
                    End If
                End If
                SearchForDuplicates(xNode, BookmarkPath, BookmarkName)
                i += 1
            End While
        End If
    End Sub

    Sub SearchForDeadLinks(ByVal inXmlNode As XmlNode)
        Dim xNode As XmlNode
        Dim nodeList As XmlNodeList
        Dim i As Integer
        If inXmlNode.HasChildNodes Then
            nodeList = inXmlNode.ChildNodes
            i = 0
            While i <= nodeList.Count - 1
                xNode = inXmlNode.ChildNodes(i)
                Dim NewBookmark As New TreeNode
                If xNode.Name <> "folder" Then
                    NewBookmark.Name = xNode.Attributes(1).Value & "\" & xNode.Attributes(0).Value
                    NewBookmark.Text = xNode.Attributes(0).Value
                    Dim BookmarkSite As String = AccessBookmarks.ReadBookmark(NewBookmark.Name)
                    Dim Bookmarkpath As String = NewBookmark.Name
                    Dim WebsiteToTry As String = GetHostName(BookmarkSite)
                    Try
                        Dim CanReachSite As Boolean = My.Computer.Network.Ping(WebsiteToTry)
                        If CanReachSite = False Then
                            AddToListView(Bookmarkpath, BookmarkSite, WebsiteToTry)
                        End If
                    Catch ex As Exception
                        AddToListView(Bookmarkpath, BookmarkSite, WebsiteToTry)
                        Exit Try
                    End Try
                End If
                SearchForDeadLinks(xNode)
                i += 1
            End While
        End If

    End Sub

    Sub AddToListView(ByVal BookmarkPath As String, ByVal Website As String, ByVal BookmarkName As String)
        Dim OriginalBookmark As New ListViewItem
        Dim OrigWebsite, OrigPath As New ListViewItem.ListViewSubItem
        OrigWebsite.Text = AccessBookmarks.ReadBookmark(BookmarkPath)
        OrigPath.Text = BookmarkPath
        OriginalBookmark.SubItems.Add(OrigWebsite)
        OriginalBookmark.SubItems.Add(OrigPath)
        OriginalBookmark.Name = BookmarkPath
        OriginalBookmark.Text = BookmarkName
        lstviewDuplicates.Items.Add(OriginalBookmark)
    End Sub

    Function CheckIfBookmarkAlreadyFlagged(ByVal Bookmark As String) As Boolean
        Dim BookmarkPath As New ListViewItem
        Dim FlaggedBookmarks As New ListView.ListViewItemCollection(lstviewDuplicates)
        For Each BookmarkPath In FlaggedBookmarks
            If BookmarkPath.Name = Bookmark Then
                Return True
            End If
        Next
        Return False
    End Function

    Private Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        AccessBookmarks.GetDirectoryStructure(FavoritesFolder)
        LoadBookmarks(frmMain.trvBookmarks)
        Me.Close()
    End Sub

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        Dim Bookmark As ListViewItem
        For Each Bookmark In lstviewDuplicates.SelectedItems
            AccessBookmarks.DeleteBookmark(Bookmark.Name)
        Next
        AccessBookmarks.GetDirectoryStructure(FavoritesFolder)
        LoadBookmarks(frmMain.trvBookmarks)
        Me.Close()
    End Sub
End Class